package com.core.family.admin.controller;

import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.common.manger.wechat.WxOrderService;
import com.core.common.utils.MyRandomUtil;
import com.core.family.admin.domain.req.PlatAddressReq;
import com.core.family.biz.yizhao.service.YzWalletService;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import com.core.family.admin.domain.po.PlatFinanceStatement;
import com.core.family.admin.service.PlatFinanceStatementService;
import com.core.family.admin.domain.req.PlatFinanceStatementReq;
import com.core.common.annotation.MenuDesc;
import com.core.common.domain.response.R;
import io.swagger.annotations.Api;

import java.util.Date;
import java.util.Map;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-7-13 15:03:14
 * @Description 服务实现层 财务账单
 */
@MenuDesc(desc = "财务账单", code = "platFinanceStatement")
@Api(tags = "PlatFinanceStatementController 财务账单")
@RestController
@RequestMapping("/api/")
public class PlatFinanceStatementController {

    @Autowired
    PlatFinanceStatementService service;

    @Autowired
    YzWalletService walletService;

    @Autowired
    private WxOrderService wxOrderService;



    /**
     * 根据id获取详情
     * @param request
     * @param id
     * @return
     */
    @GetMapping(value = "PlatFinanceStatement/get/{id}")
    public R get(HttpServletRequest request, @PathVariable("id") Long id){
        return R.ok(service.getDetailById(id));
    }
    /**
     * 分页查询
     * @return
     */
    @RequestMapping(value = "PlatFinanceStatement/getPage",method = {RequestMethod.GET,RequestMethod.POST})
    public R getPage(@RequestBody PageQuery<PlatFinanceStatementReq> pageQuery){

        PageQuery<BaseQuery> baseQuery=new PageQuery<>();
        baseQuery.setQueryBean(pageQuery.getQueryBean());
        baseQuery.setPageIndex(pageQuery.getPageIndex());
        baseQuery.setPageSize(pageQuery.getPageSize());
        return R.ok(service.selectPage(baseQuery));
    }

    /**
    * 批量查询
    * @return
    */
    @RequestMapping(value = "PlatFinanceStatement/getList",method = {RequestMethod.GET,RequestMethod.POST})
    public R getList(@RequestBody PlatFinanceStatementReq queryReq){
        return R.ok(service.selectList(queryReq));
    }


    /**
     * 批量查询
     * @return
     */
    @RequestMapping(value = "yzWallet/getWallet",method = {RequestMethod.GET,RequestMethod.POST})
    public R getWallet(@RequestBody PlatFinanceStatementReq queryReq){
        return R.ok(service.getWallet(queryReq));
    }

    /**
     * 一招充值
     * @param
     * @return
     */
    @PostMapping(value = "yzWallet/recharge")
    public R recharge(@RequestBody  Map<String,String> map, HttpServletRequest request){
        WxPayMpOrderResult result=null;
        if (map.containsKey("type") && "h5".equals(map.get("type"))){
            result=walletService.h5pay(map, request);

        }else {
            result=walletService.walletRecharge(map, request);
        }


        return R.ok(result);
    }


    /**
     * 退款
     * @param
     * @return
     */
    @PostMapping(value = "yzWallet/refund")
    public R refund(@RequestBody  Map<String,String> map, HttpServletRequest request) {
        PlatFinanceStatement statement = walletService.walletRefund(map, request);
        return R.ok(statement);
    }

    /**
     * 商户转账到用户
     * @param
     * @return
     */
    @PostMapping(value = "yzWallet/merchant/transfer")
    public R transfer(@RequestBody  PlatFinanceStatement statement){

        return R.ok();
    }

    /**
     *
     * @param
     * @return
     */
    @PostMapping(value = "yzWallet/refundAudit")
    public R refundAudit(@RequestBody  PlatFinanceStatement statement){
        statement.setAuditStatus("审核通过");
        service.updateById(statement);
        Object object= wxOrderService.refund(statement.getOutTradeno(),statement.getPaySn(),statement.getPrice().intValue());

        return R.ok(object);
    }





    }
